<template>
  <div class="custom-cascader">
    <div class="selector" @click="toggleMenu">
      <span>{{ currentLabel }}</span>
    </div>
    <transition name="fade">
      <div v-if="showMenu" class="dropdown-container">
        <ul class="dropdown-menu">
          <li 
            v-for="(item, index) in options" 
            :key="index" 
            @click="selectMainColor(item)"
            class="main-color-option"
          >
            <span class="color-marker" :style="{ backgroundColor: item.colorCode }"></span>
            {{ item.label }}
            <ul v-if="expandedOptions.includes(item.value)" class="sub-colors">
              <li 
                v-for="(subItem, subIndex) in item.children" 
                :key="`${index}-${subIndex}`" 
                @click="handleSubSelect(subItem)"
              >
                <span class="color-marker" :style="{ backgroundColor: subItem.colorCode }"></span>
                {{ subItem.label }}
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </transition>
  </div>
</template>

<script>

export default {

props: {
  options: {
    type: Array,
    required: true,
  },
  modelValue: {
    type: Object,
    default: () => ({})
  },
},
// emits: ['update:modelValue'],
data() {
  return {
    showMenu: false,
    currentLabel: '',
    expandedOptions: [], // 用于记录展开的主色系
  };
},
methods: {
  toggleMenu() {
    this.showMenu = !this.showMenu;
  },
  handleSelect(item) {
    this.currentLabel = item.label;
    // this.$emit('update:modelValue', item);
    this.showMenu = false;
  },
  selectMainColor(item) {
    // 收起其他展开的主色系，仅展开当前点击的
    this.expandedOptions = item.children ? [item.value] : [];
    // this.currentLabel = item.label;//不显示色系
    // this.$emit('update:modelValue', item);
  },
  handleSubSelect(subItem) {
    this.currentLabel = subItem.label;
    // this.$emit('update:modelValue', subItem);
    this.$emit('input', subItem); 
    this.showMenu = false;
  },
},
watch: {
  modelValue(newValue) {
    this.currentLabel = newValue.label || '';
  },
},
mounted() {
  if (this.modelValue.label) {
    this.currentLabel = this.modelValue.label;
  }
},
};
</script>

<style scoped>
.custom-cascader {
  position: relative;
  /* width: 200px; */
}

.selector {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  /* width: 153px; */
  height: 40px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 1);
  border: 2px solid rgba(211, 218, 230, 1);
}

.arrow {
  margin-left: 5px;
}

.dropdown-menu {
  position: absolute;
  top: calc(100% + 5px);
  width: 400px;
  max-height: 300px;
  overflow-y: auto;
  list-style-type: none;
  padding: 20px;
  margin: 0;
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 100;
}

/* .fade-enter-active, .fade-leave-active {
  transition: opacity .3s;
} */
.fade-enter, .fade-leave-to {
  opacity: 0;
}

.color-marker {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 5px;
  margin-left: 5px;
}
.main-color-option {
  position: relative;
}

.sub-colors {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  position: absolute;
  top: 0;
  left: 30%;
  min-width: 150px;
  list-style-type: none;
}
</style>